package vn.allmobile.ambnews.customview;

import com.aphidmobile.flip.FlipViewController;

import vn.allmobile.ambnews.ViewerActivity;
import vn.allmobile.ambnews.activities.detail.DetailActivity;
import vn.allmobile.ambnews.data.Article;
import vn.allmobile.ambnews.network.NetworkAPI;
import vn.allmobile.ambnews.utils.Log;


import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.text.TextUtils;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

public class WebviewDetailClient extends WebViewClient {
	Context _context;
	int activeLoadingCount = 0;
	FlipViewController _flipcontroller;
	int _pageCount;

	DetailActivity _activity;
	public WebviewDetailClient(Context context,DetailActivity activity) {
		// TODO Auto-generated constructor stub
		_context = context;
		_activity 		= activity;
		
	}
	public WebviewDetailClient(Context context) {
		// TODO Auto-generated constructor stub
		_context = context;		
		_activity = null;
	}
	
	@Override
	public void onReceivedError(WebView view, int errorCode,
	           String description, String failingUrl){
		super.onReceivedError(view, errorCode, description, failingUrl);
		Log.v("description:" + description);
		Log.v("errorCode:" + errorCode);
		if(errorCode == ERROR_FILE_NOT_FOUND)
		{
			view.clearCache(true);
			view.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ONLY);
			view.reload();
		}		
	}
	 
	  @Override
      public boolean shouldOverrideUrlLoading(WebView view,String strurl) {
		  	Uri uri = Uri.parse(strurl);
	        String scheme = uri.getScheme();
	        
	        if(strurl.endsWith("3gp") || strurl.endsWith("mp4"))
	        {
	        	if(strurl.contains("news.allmobile.vn"))
	        	{
	        		Article article = _activity.article;
		        	String baseUrl = article.getUrl();
		        	Uri baseUri = Uri.parse(baseUrl);
		        	String host = baseUri.getHost();
		        	if(host.equals("24h.com.vn"))
		        	{
		        		host = "hcm.m.24h.com.vn";
		        	}
		        	strurl = strurl.replace("news.allmobile.vn", host);
		        	
	        	}
	        	Intent it = new Intent(_context,ViewerActivity.class);
        		it.putExtra("vieweractivity:url", strurl);
        		it.putExtra("vieweractivity:type", "video");
        		_activity.startActivity(it);
	        	
	        	return true;
	        }
	        
	        if (scheme.equals("ambflip")) {
	            //doCustomStuff(URI.create(strurl));
	        	///Log.v("ambflip:" + strurl);
	        	String action = uri.getHost();
	        	Log.v("ambflip: action :" + action);
	        	
	        	
	        	if(action.equals("update-snapshot"))
	        	{
	        		String imgSrc = uri.getQueryParameter("src");
	        		String imgCaption = uri.getQueryParameter("caption");
	        		if(!TextUtils.isEmpty(imgSrc) && imgSrc != null && !imgSrc.equals("null"))
	        			NetworkAPI._imageList.add(imgSrc);	        		
	        		
	        	}
	        	if(action.equals("update-pagecount"))
	        	{
	        		String pageCount = uri.getQueryParameter("pageCount");
	        		Log.v("ambflip: pageCount :" + pageCount);	        		
	        		if(view == null)
	        		{
	        			Log.v("ambflip: view null:");
	        		}
	        		else
	        		{
	        			//_flipAdapter.addWebviewTolist(view);
	        		}
	        	}
	        	else if(action.equals("pagination-did-finish"))
	        	{
	        		String pageCount = uri.getQueryParameter("totalPage");
	        		Log.v("ambflip: pageCount :" + pageCount);
	        		_activity._totalPage = Integer.parseInt(pageCount);
	        		_activity._currentPage = 0;	        		
	        		//Variables.NUM_PAGE = Integer.parseInt(pageCount);
	        		 _activity._isLoaded = 1;
	        		 _activity.loadingPanel.setVisibility(View.GONE);
	        		//adpt.setPageCount(Integer.parseInt(pageCount));
	        		//_flipAdapter.notifyDataSetChanged();
	        		
	        	//	_activity.addContentView(_flipcontroller, new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
	        	//	_flipcontroller.setAdapter(_flipAdapter);
	        			       
	        	}
	        	else if(action.equals("showImage"))
	        	{
	        		if(NetworkAPI._imageList.size()>0)
	        		{
	        			String url = uri.getQueryParameter("url");	        	
		        		Intent it = new Intent(_context,ViewerActivity.class);
		        		it.putExtra("vieweractivity:url", url);
		        		it.putExtra("vieweractivity:type", "image");
		        		_activity.startActivity(it);
	        		}
	        		
	        	}
	        	view.loadUrl("javascript: window.SLBridge.setReady(true);");
	            return true;
	        }

	        Uri new_uri = Uri.parse(strurl);
			Intent intent = new Intent(Intent.ACTION_VIEW, new_uri);
			_activity.startActivity(intent);
			return true; 
      }
        	    	  
  @Override
  public void onPageStarted(WebView view, String strurl, Bitmap favicon) {	 	
	  super.onPageStarted(view,strurl, favicon);	  
  }

  @Override
	  public void onPageFinished(WebView view, String url) {
	  //	_flipcontroller.refreshPage(view);//This works as the webView is the view for a page. Please use refreshPage(int pageIndex) if the webview is only a part of page view.
	    //view.loadUrl("javascript:window.flipboardNativeFlip = true;window.isAndroid = true;window.flipboardMagazine = true;window.flipboardBridgeEnabled = true;window.flipboardShowImageEnabled = true;window.flipboardPageSizes = {portrait:{height:508.0, width:320.0}, landscape:{height:800, width:1280}};window.flipboardSocialBarHeight = 60.0;window.flipboardOrientation = 0;flipboardPrintSize=\"normal\"");
	      Log.v("onPageFinished");
	      
	    //  view.loadUrl("javascript: window.SLBridge.setReady(true);");      	    
	      
          
	    	 
          
	    
	  }
	
  

}
